import { useCallback, useEffect, useEffectEvent, useRef, useState } from 'react'
import { Dev } from '@/components/types'

interface UserCounterReturn {
    counter: number,
    increment: Dev.VF,
    decrement: Dev.VF,
    reset: Dev.VF
}

export function UseCounter (init: number): UserCounterReturn {
    const [ counter, setCounter ] = useState(init)

    const increment: Dev.VF = useCallback(() => {
        setCounter(prevState => prevState + 1)
    }, [])

    const decrement: Dev.VF = useCallback(() => (setCounter(prevState => prevState - 1)), [])

    const reset: Dev.VF = useCallback(() => (setCounter(init)), [])

    return {
        counter,
        increment,
        decrement,
        reset,
    }

}

export function useUnMounted (cb: () => void) {
    //const effectEvent = useEffectEvent(() => cb())

    const ref = useRef(cb)

    ref.current = cb

    useEffect(() => {
        return () => ref.current()
    }, [])

}
